package cn.com.surker.workcon.service.bcs;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.TaskService;
import cn.com.surker.common.Code;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 中青林工控机的任务单 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class TaskServiceImpl extends GKJDBCService implements TaskService {

	public int insert(Long lineId, ProductionIndentVo indent, Date datTim) {
		if (lineId == null || indent == null) {
			System.err.println("没有生产线(" + lineId + ")或者任务单信息(" + indent + ")");
			return Code.FAILED.getCode();
		}
		//	获取上传的生产线
		List<LabExpendUpload> expendUploadLines = indent.getExpendUploadLines();
		if (expendUploadLines == null || expendUploadLines.isEmpty()) {
			System.err.println("没有传入任务单的生产线");
			return Code.FAILED.getCode();
		}
		SysLine line = getLine(lineId);
		//	中青林的特殊性,1,2线为1个库,3,4线为1个库
		String recipe = "";
		String recipes = "";
		String code = line.getCode();
		//	判断是上传的和是否同一个库
		for (LabExpendUpload labExpendUpload : expendUploadLines) {
			Long uploadLine = labExpendUpload.getUploadLine();
			if (lineId.equals(uploadLine)) {
				//	是同一个,判断是单数还是双数
				Long controlLineId = line.getControlLineId();
				if (controlLineId.intValue() % 2 == 0) {
					//	2线
					recipes = labExpendUpload.getCode();
				} else {
					recipe = labExpendUpload.getCode();
				}
			}
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	插入sql
		String insertSql = "insert into Task (code,station,DatTim,attribute,contract,customer,ProjName,ProjType,ProjAdr,distance,ConsPos,pour,betlev,lands,mete,BegTim,EndTim,attamper,flag,note,recipe,recipes) " +
			"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Long station = line.getControlLineId();
		String attribute = "";
		String indentCode = indent.getCode();
		String contract = indent.getConName();
		String customer = indent.getClientName();
		String projName = indent.getConName();
		String projType = indent.getConCategoryTxt();
		String projAdr = indent.getAddress();
		Double distance = indent.getDistance();
		String consPos = indent.getSite();
		String pour = indent.getCastModelName();
		String betlev = indent.getStrengthName();
		String lands = indent.getSlumps();
		Double mete = indent.getPlanStere();
		Date begTim = indent.getBuilderStartTime();
		Date endTim = indent.getEndTime();
		String attamper = indent.getCreator();
		String remark = indent.getRemark();
		//	参数
		Object[] params = {indentCode, station, datTim, attribute, contract, customer, projName, projType, projAdr, distance, consPos, pour, betlev, lands, mete, begTim, endTim, attamper, 1, remark, recipe, recipes};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int update = lineJdbcTemp.update(insertSql, params);
		return update;
	}

}
